<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <title>使用nginx搭建https服务器  &middot; 程序猿的小本本</title>
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="HandheldFriendly" content="True">
<meta name="MobileOptimized" content="320">
<meta name="viewport" content="width=device-width, initial-scale=1">


<meta name="description" content="光说有毛用|程序猿的小本本" />

<meta name="keywords" content="">


<meta property="og:title" content="使用nginx搭建https服务器  &middot; 程序猿的小本本 ">
<meta property="og:site_name" content="程序猿的小本本"/>
<meta property="og:url" content="http://replace-this-with-your-hugo-site.com/2016/01/10/using_https_in_nginx.html" />
<meta property="og:locale" content="en-us">


<meta property="og:type" content="article" />
<meta property="og:description" content=""/>
<meta property="og:article:published_time" content="2016-01-10T10:33:53Z" />
<meta property="og:article:modified_time" content="2016-01-10T10:33:53Z" />

  

  
<meta name="twitter:card" content="summary" />
<meta name="twitter:site" content="@" />
<meta name="twitter:creator" content="@" />
<meta name="twitter:title" content="使用nginx搭建https服务器" />
<meta name="twitter:description" content="" />
<meta name="twitter:url" content="http://replace-this-with-your-hugo-site.com/2016/01/10/using_https_in_nginx.html" />
<meta name="twitter:domain" content="http://replace-this-with-your-hugo-site.com/">
  

<script type="application/ld+json">
  {
    "@context": "http://schema.org",
    "@type": "Article",
    "headline": "使用nginx搭建https服务器",
    "author": {
      "@type": "Person",
      "name": "http://profiles.google.com/+?rel=author"
    },
    "datePublished": "2016-01-10",
    "description": "",
    "wordCount":  80 
  }
</script>



<link rel="canonical" href="http://replace-this-with-your-hugo-site.com/2016/01/10/using_https_in_nginx.html" />

<link rel="apple-touch-icon-precomposed" sizes="144x144" href="http://replace-this-with-your-hugo-site.com/touch-icon-144-precomposed.png">
<link href="http://replace-this-with-your-hugo-site.com/favicon.png" rel="icon">
<meta name="theme-color" content="#2053AB">
<meta name="generator" content="Hugo 0.17-DEV" />

  <!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->

<link href='https://fonts.googleapis.com/css?family=Merriweather:300%7CRaleway%7COpen+Sans' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="http://replace-this-with-your-hugo-site.com/css/font-awesome.min.css">
<link rel="stylesheet" href="http://replace-this-with-your-hugo-site.com/css/style.css">
<link rel="stylesheet" href="http://replace-this-with-your-hugo-site.com/css/highlight/default.css">

  
</head>
<body>
  <main id="main-wrapper" class="container main_wrapper has-sidebar">
    <header id="main-header" class="container main_header">
  <div class="container brand">
  <div class="container title h1-like">
  <a class="baselink" href="http://replace-this-with-your-hugo-site.com/">
  程序猿的小本本

</a>

</div>

  
<div class="container topline">
  
  光说有毛用


</div>


</div>

  <nav class="container nav primary no-print">
  

<a class="homelink" href="http://replace-this-with-your-hugo-site.com/">Home</a>


  
<a href="http://replace-this-with-your-hugo-site.com/about">About</a>

<a href="http://replace-this-with-your-hugo-site.com/post" title="Show list of posts">Posts</a>

<a href="http://replace-this-with-your-hugo-site.com/tags" title="Show list of tags">Tags</a>


</nav>

<div class="container nav secondary no-print">
  
<a id="contact-link-email" class="contact_link" href="mailto:chennqqi@qq.com">
  <span class="fa fa-envelope-square"></span><span>email</span></a>



<a id="contact-link-github" class="contact_link" href="https://github.com/chennqqi?tab=repositories">
  <span class="fa fa-github-square"></span><span>github</span></a>

















</div>


  

</header>


<article id="main-content" class="container main_content single">
  <header class="container hat">
  <h1>使用nginx搭建https服务器
</h1>

  <div class="metas">
<time datetime="2016-01-10">10 Jan, 2016</time>


  
    &middot; by admin
  
  &middot; Read in about 1 min
  &middot; (80 Words)
  <br>
  


</div>

</header>

  <div class="container content">
  <p><span style="color: black; font-size: 10pt;"><span style="font-family: 微软雅黑;">最近在研究</span><span style="font-family: Verdana;">nginx</span><span style="font-family: 微软雅黑;">，整好遇到一个需求就是希望服务器与客户端之间传输内容是加密的，防止中间监听泄露信息，但是去证书服务商那边申请证书又不合算，因为访问服务器的都是内部人士，所以自己给自己颁发证书，忽略掉浏览器的不信任警报即可。下面是颁发证书和配置过程。</span><br /> </span></p>

<p><span style="color: black; font-size: 10pt;"><span style="font-family: 微软雅黑;">首先确保机器上安装了</span><span style="font-family: Verdana;">openssl</span><span style="font-family: 微软雅黑;">和</span><span style="font-family: Verdana;">openssl-devel</span><br /> </span></p>

<pre class="lang:default decode:true ">#yum install openssl
#yum install openssl-devel</pre>

<p>&nbsp;</p>

<p><span style="color: black; font-family: 微软雅黑; font-size: 10pt;">然后就是自己颁发证书给自己<br /> </span></p>

<pre class="lang:default decode:true">#cd /usr/local/nginx/conf
#openssl genrsa -des3 -out server.key 1024
#openssl req -new -key server.key -out server.csr
#openssl rsa -in server.key -out server_nopwd.key
#openssl x509 -req -days 365 -in server.csr -signkey server_nopwd.key -out server.crt</pre>

<p><span style="color: black; font-size: 10pt;"><span style="font-family: 微软雅黑;">至此证书已经生成完毕，下面就是配置</span><span style="font-family: Verdana;">nginx</span><br /> </span></p>

<pre class="lang:default decode:true ">server {
listen 443;
ssl on;
ssl_certificate /usr/local/nginx/conf/server.crt;
ssl_certificate_key /usr/local/nginx/conf/server_nopwd.key;
}</pre>

<p>&nbsp;</p>

<p><span style="color: black; font-size: 10pt;"><span style="font-family: 微软雅黑;">然后重启</span><span style="font-family: Verdana;">nginx</span><span style="font-family: 微软雅黑;">即可。</span><br /> </span></p>

<p><span style="color: black; font-size: 10pt;"><span style="font-family: Verdana;">ps</span><span style="font-family: 微软雅黑;">： 如果出现&#8221;</span><span style="font-family: Verdana;">[emerg] 10464#0: unknown directive &#8220;ssl&#8221; in /usr/local/nginx-0.6.32/conf/nginx.conf:74</span><span style="font-family: 微软雅黑;">&#8220;则说明没有将</span><span style="font-family: Verdana;">ssl</span><span style="font-family: 微软雅黑;">模块编译进</span><span style="font-family: Verdana;">nginx</span><span style="font-family: 微软雅黑;">，在</span><span style="font-family: Verdana;">configure</span><span style="font-family: 微软雅黑;">的时候加上&#8221;</span><span style="font-family: Verdana;">&#8211;with-http_ssl_module</span><span style="font-family: 微软雅黑;">&#8220;即可</span><span style="font-family: Verdana;">^^</span><br /> </span></p>

<p><span style="color: black; font-size: 10pt;"><span style="font-family: 微软雅黑;">至此已经完成了</span><span style="font-family: Verdana;">https</span><span style="font-family: 微软雅黑;">服务器搭建，但如何让浏览器信任自己颁发的证书呢？</span><br /> </span></p>

<p><span style="color: black; font-size: 10pt;"><span style="font-family: 微软雅黑;">今天终于研究捣鼓出来了，只要将之前生成的</span><span style="font-family: Verdana;">server.crt</span><span style="font-family: 微软雅黑;">文件导入到系统的证书管理器就行了，具体方法：</span><br /> </span></p>

<p><span style="color: black; font-size: 10pt;"><span style="font-family: 微软雅黑;">控制面板</span><span style="font-family: Verdana;"> -&gt; Internet</span><span style="font-family: 微软雅黑;">选项</span><span style="font-family: Verdana;"> -&gt; </span><span style="font-family: 微软雅黑;">内容</span><span style="font-family: Verdana;"> -&gt; </span><span style="font-family: 微软雅黑;">发行者 </span><span style="font-family: Verdana;">-&gt; </span><span style="font-family: 微软雅黑;">受信任的根证书颁发机构</span><span style="font-family: Verdana;"> -&gt; </span><span style="font-family: 微软雅黑;">导入</span><span style="font-family: Verdana;"> &#8211;</span><span style="font-family: 微软雅黑;">》选择</span><span style="font-family: Verdana;">server.crt</span><br /> </span></p>

<p><span style="color: #595959; font-size: 9pt;">来自 &lt;<a href="https://www.cnblogs.com/tintin1926/archive/2012/07/12/2587311.html">https://www.cnblogs.com/tintin1926/archive/2012/07/12/2587311.html</a>&gt;<br /> </span></p>

</div>


  <footer class="container">
  <div class="container navigation no-print">
  <h2>Navigation</h2>
  
  

    
    <a class="prev" href="http://replace-this-with-your-hugo-site.com/2015/12/30/redis-delkey.html" title="Redis 删除所有键值 | redis,删除key,key">
      Previous
    </a>
    

    
    <a class="next" href="http://replace-this-with-your-hugo-site.com/2016/01/11/centos_install_gitolite.html" title="centos安装gitolite">
      Next
    </a>
    

  


</div>

  <div class="container comments">
  <h2>Comments</h2>
  
<div id="disqus_thread"></div>
<script type="text/javascript">
  (function() {
    
    
    if (window.location.hostname == "localhost")
      return;

    var dsq = document.createElement('script'); dsq.async = true; dsq.type = 'text/javascript';
    dsq.src = '//your_disqus_shortname.disqus.com/embed.js';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
  })();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>


</div>

</footer>

</article>
      <footer id="main-footer" class="container main_footer">
  

  <div class="container nav foot no-print">
  

  <a class="toplink" href="#">back to top</a>

</div>

  <div class="container credits">
  
<div class="container footline">
  
  code with <i class='fa fa-heart'></i>


</div>


  
<div class="container copyright">
  
  &copy; 2016 gsymy.com.


</div>


</div>

</footer>

    </main>
    
<script type="text/javascript">
  (function() {
    
    
    if (window.location.hostname == "localhost")
      return;

    var dsq = document.createElement('script'); dsq.async = true; dsq.type = 'text/javascript';
    dsq.src = '//your_disqus_shortname.disqus.com/count.js';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
  })();
</script>



<script src="http://replace-this-with-your-hugo-site.com/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>



<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'Your Google Analytics tracking code', 'auto');
  ga('send', 'pageview');

</script>


    
  </body>
</html>

